﻿using Admin.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Admin.Controllers
{
    public class StudentController : ApiController
    {
        // GET: api/Student
        [HttpGet]
        public IHttpActionResult GetList(string Name, string age, int UserId = 1, int page = 1, int limit = 5)
        {
            using (CRMEntities db = new CRMEntities())
            {
                var list1 = db.Users.FirstOrDefault(p => p.Id == UserId);
                var datalist = db.Students.Include("Users").AsEnumerable();
                if (!string.IsNullOrEmpty(Name))
                {
                    datalist = datalist.Where(p => p.Name.Contains(Name));
                }
                if (!string.IsNullOrEmpty(age))
                {
                    datalist = datalist.Where(p => p.Age == Convert.ToInt32(age));
                }
                if (list1 != null)
                {
                    datalist = datalist.Where(p => p.AskerId == list1.Id);
                }
                var count = datalist.Count();
                var list = datalist.Select(p => new {
                    Id = p.Id,
                    StudentId = p.Id,
                    Name = p.Name,
                    StudentName = p.Name,
                    Age = p.Age,
                    Sex = p.Sex,
                    Phone = p.Phone,
                    Weixin = p.Weixin,
                    StuStatus = p.StuStatus,
                    PerState = p.PerState,
                    AskerName = p.Users.LoginName,
                    AskerId = p.AskerId,
                    UserId = p.AskerId,
                    CreateUser = p.CreateUser,
                    QQ = p.QQ
                }).Skip((page - 1) * limit).Take(limit).ToList();
                return Json(new { count = count, data = list });
            }
        }
        [HttpPost]
        public IHttpActionResult Delete([FromBody]Students stu)
        {
            using (CRMEntities db = new CRMEntities())
            {
                var list = db.Students.FirstOrDefault(p => p.Id == stu.Id);
                var list2 = db.Netfollows.FirstOrDefault(p => p.StudentId == stu.Id);
                if (list2 == null)
                {
                    db.Students.Remove(list);
                    int n = db.SaveChanges();
                    if (n > 0)
                    {
                        return Json(new { code = 0, msg = "删除成功！" });
                    }
                    else
                    {
                        return Json(new { code = -1, msg = "删除失败！" });
                    }
                }
                else
                {
                    return Json(new { code = -2, msg = "该学生还有追踪记录，不能删除!" });
                }


            }
        }
        [HttpPost]
        public IHttpActionResult PEditAsker([FromBody]Students stu)
        {
            using (CRMEntities db = new CRMEntities())
            {
                var list = db.Students.FirstOrDefault(p => p.Id == stu.Id);
                list.AskerId = stu.AskerId;
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "修改成功！" });
                }
                else
                {
                    return Json(new { code = -1, msg = "修改失败！" });
                }
            }
        }
        [HttpPost]
        public IHttpActionResult AddStu([FromBody]Students add)
        {
            using (CRMEntities db = new CRMEntities())
            {
                db.Students.Add(add);
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "添加成功！" });
                }
                else
                {
                    return Json(new { code = -1, msg = "添加失败！" });
                }
            }
        }
        [HttpPost]
        public IHttpActionResult EditStu([FromBody]Students add)
        {
            using (CRMEntities db = new CRMEntities())
            {
                var list = db.Students.FirstOrDefault(p => p.Id == add.Id);
                list.Name = add.Name;
                list.Age = add.Age;
                list.Sex = add.Sex;
                list.Phone = add.Phone;
                list.Weixin = add.Weixin;
                list.StuStatus = add.StuStatus;
                list.PerState = add.PerState;
                list.AskerId = add.AskerId;
                list.IsReturnVist = add.IsReturnVist;
                list.QQ = add.QQ;
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "编辑成功！" });
                }
                else
                {
                    return Json(new { code = -1, msg = "编辑失败！" });
                }

            }
        }
        [HttpPost]
        public IHttpActionResult GenUser([FromBody] Netfollows stu)
        {
            using (CRMEntities db = new CRMEntities())
            {
                db.Netfollows.Add(stu);
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "跟踪成功！" });
                }
                else
                {
                    return Json(new { code = -1, msg = "跟踪失败！" });
                }
            }
        }
    }
}
